package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {


    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean){
        //获取当前页数
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示条数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //分页
        PageHelper.startPage(currentPage,pageSize);
        //根据条件查询
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public CheckItem selectById(Integer id) {
        CheckItem checkItem = checkItemDao.selectById(id);
        return  checkItem;
    }

    @Override
    public void update(CheckItem checkItem) {
       checkItemDao.edit(checkItem);
    }

    @Override
    public void delete(Integer id) {

        Long count = checkItemDao.findByCheckItemId(id);
        //判断该检查项是否已关联到检查组
        if (count > 0) {
            //该检查项已被关联进检查组,不允许被删除
            new RuntimeException();
        }
        checkItemDao.del(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
